package Framework.MQ
{
	import Framework.Serialize.*;
	
	public class MessageBlock extends Object
	{
		private var _messageHead:MessageHead;
		private var _messageBase:IMessageBase;
		
		public function MessageBlock()
		{
			this._messageHead = new MessageHead();
		}
		
		public function __write(stream:SerializeStream) : void
		{
			this.__writeHead(stream);
			this.__writeBody(stream);
		}
		
		public function __writeHead(stream:SerializeStream) : void
		{
			this._messageHead.__write(stream);
		}
		
		public function __writeBody(stream:SerializeStream) : void
		{
			if (this._messageBase)
			{
				stream.writeSize(this._messageBase.getType());
				this._messageBase.__write(stream);
			}
			else
			{
				stream.writeSize(0);
			}
		}
		
		public function __read(stream:SerializeStream) : void
		{
			this.__readHead(stream);
			this.__readBody(stream);
		}
		
		public function __readHead(stream:SerializeStream) : void
		{
			this._messageHead.__read(stream);
		}
		
		public function __readBody(stream:SerializeStream) : void
		{
			var size:* = stream.readSize();
			if (size != 0)
			{
				this._messageBase = MessageManager.instance().createMessage(size);
				this._messageBase.__read(stream);
			}
		}
		
		public function get messageHead() : MessageHead
		{
			return this._messageHead;
		}
		
		public function set messageHead(head:MessageHead) : void
		{
			this._messageHead = head;
		}
		
		public function get messageBase() : IMessageBase
		{
			return this._messageBase;
		}
		
		public function set messageBase(message:IMessageBase) : void
		{
			this._messageBase = message;
		}
		
	}
}
